/**
 * 
 */
package com.ztesoft.web.common.service.impl;

import java.util.Date;
import java.util.List;

import javax.annotation.Resource;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.ztesoft.core.common.Page;
import com.ztesoft.core.convert.IArgConversionService;
import com.ztesoft.core.idproduce.ISequenceGenerator;
import com.ztesoft.framework.exception.BaseAppException;
import com.ztesoft.framework.log.ZTEsoftLogManager;
import com.ztesoft.web.common.DMSConstant;
import com.ztesoft.web.common.db.arg.OperationLogArg;
import com.ztesoft.web.common.db.dao.OperationLogDao;
import com.ztesoft.web.common.db.po.OperationLogPO;
import com.ztesoft.web.common.service.IOperationLogService;

/**
 * <Description> <br>
 * 
 * @author codeCreater<br>
 * @version 1.0<br>
 * @taskId <br>
 * @CreateDate 2014年11月11日 <br>
 * @since V1.0<br>
 * @see com.ztesoft.web.common.service.impl <br>
 */

@Service("operationLogService")
public class OperationLogServiceImpl implements IOperationLogService {

    private static final ZTEsoftLogManager logger = ZTEsoftLogManager
            .getLogger(OperationLogServiceImpl.class);

    @Autowired
    private OperationLogDao operationLogDao;
    

    /**
     * 查询条件转换成Arg类的服务接口
     */
    @Resource(name = "defaultArgConversionService")
    private IArgConversionService argConversionService;

    /**
     * 主键生成器
     */
    @Resource(name = "sequenceProcGenerator")
    private ISequenceGenerator sequenceGenerator;
    

    @Override
    public OperationLogPO selectByPrimaryKey(Integer key) throws BaseAppException {
        // ///////
        // TODO 根据业务场景，设置查询条件、数据校验等

        // ///////
        return operationLogDao.selectByPrimaryKey(key);
    }

    @Override
    public List<OperationLogPO> selectByArg(OperationLogPO record) throws BaseAppException {
        logger.debug("selectByArg begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        //OperationLogArg arg = new OperationLogArg();
        //OperationLogCriteria criteria = arg.createCriteria();
        
        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        OperationLogArg arg = argConversionService.invokeArg(OperationLogArg.class, record);

        // ///////
        // TODO 根据业务场景，设置查询条件，示例
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }
        // ///////

        return operationLogDao.selectByArg(arg);
    }

    @Override
    public Page<OperationLogPO> selectByArgAndPage(OperationLogPO record, Page<OperationLogPO> resultPage)
            throws BaseAppException {
        logger.debug("selectByArgAndPage begin...record={0}", record);

        // 第一种方式：自己创建arg，自行设置查询条件及操作符
        // OperationLogArg arg = new OperationLogArg();
        // //TODO 根据业务场景，设置查询条件，示例
        // OperationLogCriteria criteria = arg.createCriteria();
        // if (Utils.notEmpty(record.getUserName())) {
        // criteria.andUserNameLike(record.getUserName());
        // }

        // 第二种方式：利用arg转换服务，转换出arg，带上查询条件及操作符，
        // 转换后，还可以自行对arg进行设置修改
        OperationLogArg arg = argConversionService.invokeArg(OperationLogArg.class, record);

        resultPage = operationLogDao.selectByArgAndPage(arg, resultPage);


        return resultPage;
    }

    @Override
    public int add(OperationLogPO record) throws BaseAppException {
        logger.debug("add begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        // int pkId = sequenceGenerator.sequenceIntValue("表名","主键名");
        // record.setUserId(pkId);
        // record.setCreatedDate(new Date());
        // ///////

        return operationLogDao.insertSelective(record);
    }

    @Override
    public int update(OperationLogPO record) throws BaseAppException {
        logger.debug("update begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行重名校验、数据有效性校验、设置属性默认值等

        // ///////

        return operationLogDao.updateByPrimaryKeySelective(record);
    }

    @Override
    public int delete(OperationLogPO record) throws BaseAppException {
        logger.debug("delete begin...record={0}", record);

        // ///////
        // TODO 根据业务场景，进行关联性校验、关联删除等

        // ///////

        return operationLogDao.deleteByPrimaryKey(record.getLogId());
    }
    
    @Override
    public int addOperationLogPO(Integer opType, String opDesc) throws BaseAppException {
        logger.debug("add begin...record={0}", "opType.." + opType + "opDesc.." + opDesc);

        // ///////
        // TODO 根据业务场景，进行重名校验、设置主键、设置属性默认值等
        // 获取主键
        OperationLogPO record = new OperationLogPO();
        int pkId = sequenceGenerator.sequenceIntValue("OPERATION_LOG","LOG_ID");
        record.setLogId(pkId);
        record.setStaffId(-1);
        record.setOpModule("系统启动初始化");
        record.setOpType(opType);
        record.setOpDesc(opDesc);
        record.setOpDate(new Date());
        record.setLogType(0);
        record.setLogLevel(1);

        return operationLogDao.insertSelective(record);
    }
    

    @Override
    public int add(String opDesc) {
    	if (DMSConstant.WRITE_OPER_LOG) {    		
    		OperationLogPO record = new OperationLogPO();
    		int pkId = 99999;
			try {
				pkId = sequenceGenerator.sequenceIntValue("OPERATION_LOG","LOG_ID");
			} catch (BaseAppException e) {
			}
    		record.setLogId(pkId);
    		record.setStaffId(-1);
    		record.setOpModule("DATA_AUDIT");
    		record.setOpType(3);
    		record.setOpDesc(opDesc);
    		record.setOpDate(new Date());
    		record.setLogType(0);
    		record.setLogLevel(1);
    		
    		return operationLogDao.insertSelective(record);
    	} else {
    		return -1;
    	}
    }
}
